package com.gt.wxpay.cashier.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date;

public class LocalTimeUtils {

    public static final DateTimeFormatter YYYY_MM_DD_HH_MM_SS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter YYYY_MM_DD = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .append(DateTimeFormatter.ISO_OFFSET_DATE_TIME).toFormatter();

    // 2024-03-15T17:30:05+08:00 转化为Date类型
    public static final String WX_SUCCESS_TIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";

    public static String getCurrentTime() {
        return LocalDateTime.now().format(YYYY_MM_DD_HH_MM_SS);
    }

    public static String getCurrentTimeSimple() {
        return LocalDateTime.now().format(YYYY_MM_DD);
    }

    public static Date dealDateFormatWithWx(String oldDateStr) {
        String replace = oldDateStr.substring(0, 19).replace("T", " ");
        DateFormat df = new SimpleDateFormat(WX_SUCCESS_TIME_FORMATTER);
        try {
            Date date = df.parse(replace);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static LocalDateTime dealFormatWithWx(String oldDateStr) {
        String replace = oldDateStr.substring(0, 19).replace("T", " ");
        return LocalDateTime.from(YYYY_MM_DD_HH_MM_SS.parse(replace));
    }

    public static String getBeforeDaySimple() {
        return LocalDateTime.now().minusDays(1).format(YYYY_MM_DD);
    }

    /**
     * 获取5秒后的时间
     *
     * @return String
     */
    public static String getAfterFiveSeconds() {
        return LocalDateTime.now().plusSeconds(5).format(YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 获取5秒前的时间
     *
     * @return String
     */
    public static String getBeforeFiveSeconds() {
        return LocalDateTime.now().minusSeconds(5).format(YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 获取X秒前的时间
     *
     * @return String
     */
    public static String getBeforeThatSeconds(int seconds) {
        return LocalDateTime.now().minusSeconds(seconds).format(YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 获取X秒前的时间
     *
     * @return String
     */
    public static String getBeforeThirtyMinutes() {
        return LocalDateTime.now().minusMinutes(30).format(YYYY_MM_DD_HH_MM_SS);
    }

    public static String getBeforeMonthsSimple() {
        return LocalDateTime.now().minusMonths(1).format(YYYY_MM_DD);
    }

    public static String getBeforeWeekSimple() {
        return LocalDateTime.now().minusWeeks(1).format(YYYY_MM_DD);
    }
}
